package com.dai.designpattern.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author daijiyong
 */
public class ContainerSingleton {

    private static Map<String, Object> ioc = new ConcurrentHashMap<String, Object>(16);

    private ContainerSingleton() {
    }

    public static Object getInstance(String className) {
        if (!ioc.containsKey(className)) {
            try {
                synchronized (ioc) {
                    Object instance = Class.forName(className).newInstance();
                    ioc.put(className, instance);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ioc.get(className);
    }
}
